package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IPanelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class IPanelServiceImpl implements IPanelService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    /**
     * 查询推荐商品
     *
     * @param recommendPanelId
     * @return com.cskaoyan.shopping.dto.PanelDto
     * @author mabai
     * @since 2022/10/26 10:22
     */
    @Override
    public List<PanelDto> getRecommendProductByRecommendPanelId(int recommendPanelId) {
        // 首先根据recommendPanelId查询tb_panel表中的相关信息
        Panel panel = panelMapper.selectByPrimaryKey(recommendPanelId);
        panel.setLimitNum(null);
        // 再根据recommendPanelId查询tb_panel_content表中的相关信息
        List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(recommendPanelId);
        panel.setPanelContentItems(panelContentItems);
        // Panel -> PanelDto
        PanelDto panelDto = contentConverter.panel2Dto(panel);
        List<PanelDto> result = new ArrayList<>();
        result.add(panelDto);
        return result;
    }
}
